package com.bosssoft.ct.springcloud.config;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * 应用上下文配置
 *
 * @author ChenTong
 * @version 1.0
 * @date 2020/6/22 15:43
 * @copyright
 * @modified
 * @see RestTemplate
 * @since
 **/
@Configuration
public class ApplicationContextConfig {
    /**
     * 将RestTemplate注册到applicationcontext中
     * 也可以直接在Main主启动类中按照一下的方式是哟个还能@Bean直接注册
     * @author ChenTong
     * @return org.springframework.web.client.RestTemplate
     * @date 2020/6/22 15:44
     */
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }

}
